package edu.bit.water.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;

import edu.bit.water.dao.EntityEvaWsrcInfoDao;
import edu.bit.water.dao.LabelEvaWsrcInfoDao;
import edu.bit.water.entity.LabelEvaWsrcInfo;

@Service
public class EntityEvaWsrcInfoService {

	@Autowired
	private EntityEvaWsrcInfoDao entityEvaWsrcInfoDao;
	@Autowired
	private LabelEvaWsrcInfoDao labelEvaWsrcInfoDao;
	
	/**
	 * 插入數值
	 * @param list 真实數值
	 * @param listColumn 列名
	 * @return succss and failed message
	 */
	public Map<String, Object> insertEntityEvaWsrcInfo(List<Object> list, List<String> listColumn) {
		Map<String, Object> insertMap = new HashMap<>();
		Map<String, Object> modelMap = new HashMap<>();
		//固定字段
		String strSum = "water_source_type,normal_water_src_design,normal_water_src_real," + 
				"normal_water_src_pip_design,nonormal_water_src_design,nonormal_water_src_real," + 
				"nonormal_water_src_pip_design";
		for(String str : listColumn)
			strSum += ',' + str;
		strSum += ",main_using,remark,company_id,user_id";//變化字段在固定字段中間
		insertMap.put("lineColumn", strSum);
		insertMap.put("lineList", list);
		try {
			entityEvaWsrcInfoDao.insertEntityEvaWsrcInfo(insertMap);
			modelMap.put("status", "success");
		}catch (DuplicateKeyException e){
        	if(e.getMessage().contains("CLASS_LABEL")) {
        		modelMap.put("status", "failed");
        		modelMap.put("msg", "水源类别已存在，请重新输入");
        		return modelMap;
        	}else if(e.getMessage().contains("EN_NAME")) {
        		modelMap.put("status", "failed");
        		modelMap.put("msg", "英文名称已存在，请重新输入");
        		return modelMap;
        	}
        }
		
		return modelMap;
	}
	
	/**
	 * 刪除數據
	 * @param id delete row id
	 * @return 1 成功；0 失败
	 */
	public int deleteEntityEvaWsrcInfo(List<Integer> listId) {
		try {
			//使用增强for循环进行删除
			for(int id : listId) {
				entityEvaWsrcInfoDao.deleteEntityEvaWsrcInfo(id);
			}
			return 1;
		} catch (Exception e) {
			e.printStackTrace();
			return 0;
		}
		
	}
	
	/**
	 * 根据label_id删除数据
	 * @param label_id 组号
	 * @return 1 成功；0 失败
	 */
	public int deleteEntityEvaWsrcInfoByLabelId(int label_id) {
		try {
			entityEvaWsrcInfoDao.deleteEntityEvaWsrcInfoByLabelId(label_id);
			return 1;
		} catch (Exception e) {
			e.printStackTrace();
			return 0;
		}
	}
	
	/**
	 * 根據label_id展示評估數據
	 * @param label_id
	 * @return database message
	 */
	public List<Map<String, Object>> selectEntityEvaWsrcInfo(int label_id,int currentUserId) {
		return entityEvaWsrcInfoDao.selectEntityEvaWsrcInfo(label_id,currentUserId);
	}
		
	/**
	 * 更新記錄
	 * @param id 記錄id
	 * @param list 記錄數據
	 * @param listColumn 記錄列名
	 * @return 成功返回1，失败返回0
	 */
	public Map<String, Object> updateEntityEvaWsrcInfo(int id, List<Object> list, List<String> listColumn) {
		Map<String, Object> modelMap = new HashMap<>();
		int length = listColumn.size();
		//可變字段在固定字段中間
		String strSum = "water_source_type=" + "'" + list.remove(0) + "'," +
				"normal_water_src_design=" + "'" + list.remove(0) + "'," +
				"normal_water_src_real=" + "'" + list.remove(0) + "'," +
				"normal_water_src_pip_design=" + "'" + list.remove(0) + "'," +
				"nonormal_water_src_design=" + "'" + list.remove(0) + "'," +
				"nonormal_water_src_real=" + "'" + list.remove(0) + "'," +
				"nonormal_water_src_pip_design=" + "'" + list.remove(0) + "',";
				
		for(int i = 0; i < length; i++) {
			strSum += listColumn.get(i) + "='" + list.remove(0) + "',";
 		}
		strSum += "main_using=" + "'" + list.remove(0) + "'," + 
				"remark=" + "'" + list.remove(0) + "',"+
				"user_id=" + "'" + list.remove(0) + "',";
		strSum = strSum.substring(0, strSum.length() - 1);
		Map<String, Object> sqlMap = new HashMap<>();
		sqlMap.put("id", id);
		sqlMap.put("sql", strSum);
		try {
			entityEvaWsrcInfoDao.updateEntityEvaWsrcInfo(sqlMap);
			modelMap.put("status", "success");
		}catch (DuplicateKeyException e){
        	if(e.getMessage().contains("CLASS_LABEL")) {
        		modelMap.put("status", "failed");
        		modelMap.put("msg", "水源类别已存在，请重新输入");
        		return modelMap;
//        	}else if(e.getMessage().contains("EN_NAME")) {
//        		modelMap.put("status", "failed");
//        		modelMap.put("msg", "英文名称已存在，请重新输入");
//        		return modelMap;
        	}
        }
		return modelMap;
	}
	
	public Map<String, Object> selectEntityEvaWsrcInfoSum(int label_id,int currentUserId) {
		try {
			List<Map<String, Object>> sqlMap = entityEvaWsrcInfoDao.selectEntityEvaWsrcInfoSum(label_id,currentUserId);
			
			LabelEvaWsrcInfo labelEvaWsrcInfo = new LabelEvaWsrcInfo();
			labelEvaWsrcInfo.setNwSum1(Double.valueOf(sqlMap.get(0).get("NWSUM1").toString()));
			labelEvaWsrcInfo.setNwSum2(Double.valueOf(sqlMap.get(0).get("NWSUM2").toString()));
			labelEvaWsrcInfo.setNwSum3(Double.valueOf(sqlMap.get(0).get("NWSUM3").toString()));
			labelEvaWsrcInfo.setnNwSum1(Double.valueOf(sqlMap.get(0).get("NNWSUM1").toString()));
			labelEvaWsrcInfo.setnNwSum2(Double.valueOf(sqlMap.get(0).get("NNWSUM2").toString()));
			labelEvaWsrcInfo.setnNwSum3(Double.valueOf(sqlMap.get(0).get("NNWSUM3").toString()));
			labelEvaWsrcInfo.setId(label_id);
			
			labelEvaWsrcInfoDao.updateLabelEvaWsrcInfoSum(labelEvaWsrcInfo);
			return sqlMap.get(0);
		} catch (Exception e) {
			throw e;
		}
	}
	
}
